package com.mida.app.knw.service.impl;

import com.mida.app.knw.service.DetailCodingSystemService;
import com.mida.app.knw.service.KnwConceptualDetailService;
import com.mida.dms.knw.api.KnwConceptualDetailApi;
import com.mida.dms.knw.entity.vo.KnwConceptualDetailVo;
import com.mida.dms.knw.vo.KnwConceptualDetailPageVo;
import com.mida.dms.knw.vo.PageResultVo;
import com.mida.tool.common.result.ListResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName: KnwConceptualDetailServiceImpl
 * @Description: TODO
 * @Author: wang xiao le
 * @Date: 2021/6/22 17:34
 **/
@Service
@Slf4j
@Transactional
public class KnwConceptualDetailServiceImpl implements KnwConceptualDetailService {
    @Autowired
    private KnwConceptualDetailApi knwConceptualDetailApi;
    @Autowired
    private DetailCodingSystemService detailCodingSystemService;
    /**
     * 5、分页查询概念描述
     */
    @Override
    public ListResult<PageResultVo<KnwConceptualDetailVo>> findPage(Long currentPage, Long pageSize, String searChData, Long conceptualSoid) {
        log.info("开始分页查询,查询条件:" + searChData);

        StopWatch st = StopWatch.createStarted();
        ListResult<PageResultVo<KnwConceptualDetailVo>> KnwConceptualDetailVoPage=new ListResult<>();
        try {
            if (ObjectUtils.isNotEmpty(searChData)) {
                searChData=searChData.trim().toUpperCase();
            }
             KnwConceptualDetailVoPage = knwConceptualDetailApi.findPage(currentPage,pageSize,searChData,conceptualSoid);
            List<KnwConceptualDetailVo> knwConceptualDetailVoList = KnwConceptualDetailVoPage.getData().getList();

            //补充语言，类型
            if (!CollectionUtils.isEmpty(knwConceptualDetailVoList)) {
                List<Long> languageSoidList = knwConceptualDetailVoList.stream().map(KnwConceptualDetailVo::getLanguageSoid).distinct().collect(Collectors.toList());
                Map<Long, String> languageNameMap = detailCodingSystemService.getDataSetNameMap(languageSoidList);

                List<Long> conceptualDetailTypeList = knwConceptualDetailVoList.stream().map(KnwConceptualDetailVo::getConceptualDetailType).distinct().collect(Collectors.toList());
                Map<Long, String> conceptualDetailTypeNameMap = detailCodingSystemService.getDataSetNameMap(conceptualDetailTypeList);

                knwConceptualDetailVoList.forEach(knwConceptualDetailVo -> {
                    knwConceptualDetailVo.setLanguageName(languageNameMap.get(knwConceptualDetailVo.getLanguageSoid()));
                    knwConceptualDetailVo.setConceptualDetailTypeName(conceptualDetailTypeNameMap.get(knwConceptualDetailVo.getConceptualDetailType()));
//                    return knwConceptualDetailVo;
                        });


            }
        } catch (Exception e) {


            log.error("分页查概念描述发送异常", e);

        } finally {
            st.stop();
            log.info("本次分页查询耗时:" + st.getTime() + "ms");
        }
        return KnwConceptualDetailVoPage;
    }

}
